﻿using System;
using System.Collections.Generic;
using Twins.Infrastructure;
using Twins.Interface;
using System.Linq;
namespace Twins.Classes
{
    public class SelectorDuplicationHashsLineCodeFiles : ISelectorDuplicationHashsLineCodeFiles
    {
        private List<ILineCode> duplicationHashsLineCode;

        public IEnumerable<ILineCode> SearchDuplication(IEnumerable<IHashsFile> hashsFiles)
        {
            duplicationHashsLineCode = new List<ILineCode>();
            var hashsLineCodeMethods = MergeMethodsInFiles(hashsFiles);
            for (int currenNumber = 0; currenNumber < hashsLineCodeMethods.Count() - 1; currenNumber++)
            for (int nextNumber = currenNumber+1; nextNumber < hashsLineCodeMethods.Count(); nextNumber++)
                    SearchDuplicationInMethods(hashsLineCodeMethods[currenNumber].HashsLineCode,
                                               hashsLineCodeMethods[nextNumber].HashsLineCode);
            return duplicationHashsLineCode;
        }

        private List<IHashsLineCodeMethod> MergeMethodsInFiles(IEnumerable<IHashsFile> hashsFiles)
        {
            var hashsLineCodeMethods = new List<IHashsLineCodeMethod>();
            foreach (var hashsFile in hashsFiles)
                hashsLineCodeMethods.AddRange(hashsFile.HashsLineCodeMethods);
            foreach (var lineCodeMethod in hashsLineCodeMethods.SelectMany(hashsLineCodeMethod => hashsLineCodeMethod.HashsLineCode))
                lineCodeMethod.IsHighLighting = false;
            return hashsLineCodeMethods;
        }

        private void SearchDuplicationInMethods(List<ILineCode> hashsLineCodeCurrenMethod, 
            List<ILineCode> hashsLineCodeNextMethod)
        {
            var adderLineCode = IoC.Resolve<IAdderLineCode>();
            if (hashsLineCodeCurrenMethod.Count >= 2 && hashsLineCodeNextMethod.Count >= 2)
            for (int currenNumber = 0; currenNumber < hashsLineCodeCurrenMethod.Count(); currenNumber++)
            for (int nextNumber = 0; nextNumber < hashsLineCodeNextMethod.Count(); nextNumber++)
                if(hashsLineCodeCurrenMethod[currenNumber].HashLineCode==hashsLineCodeNextMethod[nextNumber].HashLineCode)
                    if (currenNumber != hashsLineCodeCurrenMethod.Count() - 1 
                        && nextNumber != hashsLineCodeNextMethod.Count() - 1)
                        if (hashsLineCodeCurrenMethod[currenNumber+1].HashLineCode == hashsLineCodeNextMethod[nextNumber+1].HashLineCode)
                        {
                            adderLineCode.AddLineCode(duplicationHashsLineCode, hashsLineCodeCurrenMethod[currenNumber]);
                            adderLineCode.AddLineCode(duplicationHashsLineCode, hashsLineCodeCurrenMethod[currenNumber+1]);
                            adderLineCode.AddLineCode(duplicationHashsLineCode, hashsLineCodeNextMethod[nextNumber]);
                            adderLineCode.AddLineCode(duplicationHashsLineCode, hashsLineCodeNextMethod[nextNumber+1]);
                        }            
        }
    }
}